package hikari.hcomponents.physics;

import com.gooddies.wiring.Wiring;
import hikari.gui.panels.bullet.*;
import hikari.persistence.file.FileSection;
import java.io.IOException;
import org.swig.jirr.ISceneNode;
import org.swig.jirr.aabbox3df;

/**
 * @author sad
 */
public class CylinderRigidBody extends IRigidBody {

    protected float radius;
    protected float height;
    protected PhysicsData data;

    public CylinderRigidBody(ISceneNode node, PhysicsData data) {
        this.data = data;
        aabbox3df box = node.getBoundingBoxConst();
        float r1 = box.getMaxEdge().getX() - box.getMinEdge().getX();
        if (r1 < 0) {
            r1 *= -1;
        }
        float r2 = box.getMaxEdge().getZ() - box.getMinEdge().getZ();
        if (r2 < 0) {
            r2 *= -1;
        }

        radius = r1 > r2 ? r1 : r2;
        radius /= 2.0f;
        height = box.getMaxEdge().getY() - box.getMinEdge().getY();
        if (height < 0) {
            height *= -1;
        }
    }

    @Override
    public IBulletRigidBodyEditPanel getEditPanel() {
        BulletCylinderRigidBodyPanel editPanel = Wiring.getComponent(BulletCylinderRigidBodyPanel.class);
        editPanel.init(data);
        return editPanel;
    }

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    @Override
    public void write(FileSection section) throws IOException {
        FileSection sect = section.createNewSection("cylinderRigid");
        sect.writeFloat("radius", getRadius());
        sect.writeFloat("height", getHeight());
        sect.close();
    }

    @Override
    public void read(FileSection section) throws IOException {
        setRadius(section.readFloat("radius"));
        setHeight(section.readFloat("height"));
    }
}
